#include "link_queue.h"


void LinkQueueInit(Queue *Q)
{
    Q->front = Q->rear = (QueueNode*)malloc(sizeof(QueueNode));
    Q->front->next = NULL;
}

int LinkQueueIsEmpty(const Queue *queue)
{
    if(queue->front == queue->rear){
        return 1;
    }else{
        return -1;
    }
}

void LinkQueueEn(Queue *queue, const ElemType data)
{
    QueueNode * node = (QueueNode*)malloc(sizeof(QueueNode));
    node->data = data;
    node->next = NULL; /*记得将队尾置空*/
    queue->rear->next = node;
    queue->rear = node;
}

ElemType LinkQueueDe(Queue *queue)
{

    /*判断queue是否为空*/
    if(LinkQueueIsEmpty(queue) > 0){
        return NULL;
    }
    ElemType data;
    QueueNode * node = queue->front->next;
    data = node->data;
    queue->front->next = node->next;

    /*处理删除的是队列中的最后一个节点*/
    if(queue->rear == node)
        queue->rear = queue->front;
    free(node);

    return data;
}

void LinkQueueTraverse(const Queue *Q)
{
    printf("LinkQueueTraverse\n");
    Queue *queue = Q;
/*
 * 使用while(LinkQueueIsEmpty > 0)会出现程序退出问题
 * ？
 */
    while(queue->front->next != NULL){
        queue->front = queue->front->next;
        printf("%d\n",queue->front->data);
    }
}
